﻿package mortal.common.swfPlayer
{
    import com.gengine.core.frame.*;
    import com.mui.utils.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.common.swfPlayer.frames.*;

    public class SWFPlayer extends GPlayer
    {
        private var _timer:FrameTimer;
        private var _ramdomDaily:int;
        private var _timerKey:uint;
        private var _playNum:int = 0;
        private var _currentPlayNum:int = 0;
        public var actionPlayCompleteHandler:Function;
        private var _loadedPlay:Boolean = true;

        public function SWFPlayer()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            return;
        }// end function

        public function get playNum() : int
        {
            return this._playNum;
        }// end function

        public function set playNum(param1:int) : void
        {
            this._playNum = param1;
            return;
        }// end function

        private function get timer() : FrameTimer
        {
            if (!this._timer)
            {
                this._timer = new FrameTimer(timeRate);
            }
            return this._timer;
        }// end function

        protected function onAddToStage(event:Event) : void
        {
            this.timer.addListener(TimerType.ENTERFRAME, this.onEnterFrame);
            this.play();
            return;
        }// end function

        protected function onRemoveFromStage(event:Event) : void
        {
            this.stop();
            return;
        }// end function

        public function set ramdomDaily(param1:int) : void
        {
            this._ramdomDaily = param1;
            return;
        }// end function

        public function set timeRate(param1:int) : void
        {
            if (_timeRate != param1)
            {
                _timeRate = param1;
                if (this._timer)
                {
                    this._timer.delay = param1;
                }
            }
            return;
        }// end function

        public function set loadedPlay(param1:Boolean) : void
        {
            this._loadedPlay = param1;
            return;
        }// end function

        private function onEnterFrame(param1:FrameTimer) : void
        {
            nextFrame();
            updateCurrentFrame(currentFrame);
            return;
        }// end function

        override protected function framesPlayerComplete() : void
        {
            super.framesPlayerComplete();
            var _loc_1:String = this;
            var _loc_2:* = this._currentPlayNum + 1;
            _loc_1._currentPlayNum = _loc_2;
            if (this._playNum == 0)
            {
            }
            else if (this._currentPlayNum >= this._playNum)
            {
                this.stop();
                this.actionPlayComplete();
            }
            return;
        }// end function

        protected function actionPlayComplete() : void
        {
            if (this.actionPlayCompleteHandler is Function)
            {
                this.actionPlayCompleteHandler();
            }
            return;
        }// end function

        private function playDaily() : void
        {
            super.play();
            this.timer.start();
            currentFrame = 0;
            return;
        }// end function

        override public function play() : void
        {
            clearTimeout(this._timerKey);
            if (this._ramdomDaily > 0)
            {
                this._timerKey = setTimeout(this.playDaily, this._ramdomDaily * Math.random());
            }
            else
            {
                super.play();
                this.timer.start();
                currentFrame = 0;
            }
            return;
        }// end function

        override public function stop() : void
        {
            super.stop();
            if (this._timer)
            {
                this._timer.stop();
            }
            clearTimeout(this._timerKey);
            return;
        }// end function

        public function playAction(param1:int, param2:int, param3:int = 1) : void
        {
            this._playNum = param3;
            this._currentPlayNum = 0;
            this.updateFrame(param1, param2, true);
            this.play();
            return;
        }// end function

        override protected function onLoaded(param1:SwfFrames) : void
        {
            if (this.currentDirection < 1)
            {
                this.currentDirection = DirectionType.DefaultDir;
            }
            this.updateFrame(this.currentAction, this.currentDirection);
            updateCurrentFrame(0);
            super.onLoaded(param1);
            if (this._loadedPlay)
            {
                this.play();
            }
            return;
        }// end function

        override public function updateFrame(param1:int, param2:int, param3:Boolean = true) : void
        {
            super.updateFrame(param1, param2, param3);
            this.timer.delay = this.timeRate;
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.stop();
            this._currentPlayNum = 0;
            this._ramdomDaily = 0;
            this._playNum = 0;
            if (this._timer)
            {
                this._timer.dispose();
                this._timer = null;
            }
            transform.matrix = new Matrix();
            this.scaleX = 1;
            this.scaleY = 1;
            rotation = 0;
            super.dispose();
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            this._loadedPlay = true;
            this.actionPlayCompleteHandler = null;
            _url = "";
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            return;
        }// end function

    }
}
